home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / program / ixemlsrc.lha / ixemul / library / ix_startup.c < prev    next >
C/C++ Source or Header  |  1995-12-23  |  4KB  |  142 lines

  1. /*
  2.  *  This file is part of ixemul.library for the Amiga.
  3.  *  Copyright (C) 1991, 1992  Markus M. Wild
  4.  *  Portions Copyright (C) 1994 Rafael W. Luebbert
  5.  *
  6.  *  This library is free software; you can redistribute it and/or
  7.  *  modify it under the terms of the GNU Library General Public
  8.  *  License as published by the Free Software Foundation; either
  9.  *  version 2 of the License, or (at your option) any later version.
  10.  *
  11.  *  This library is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  *  Library General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU Library General Public
  17.  *  License along with this library; if not, write to the Free
  18.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  *
  20.  *  $Id: ix_startup.c,v 1.5 1994/06/19 15:13:22 rluebbert Exp $
  21.  *
  22.  *  $Log: ix_startup.c,v $
  23.  *  Revision 1.5  1994/06/19  15:13:22  rluebbert
  24.  *  *** empty log message ***
  25.  *
  26.  *  Revision 1.3  1992/08/09  20:55:51  amiga
  27.  *  import sysbase
  28.  *
  29.  *  Revision 1.2  1992/07/04  19:18:21  mwild
  30.  *  remove SIGWINCH handler before returning
  31.  *
  32.  * Revision 1.1  1992/05/14  19:55:40  mwild
  33.  * Initial revision
  34.  *
  35.  */
  36.  
  37. #define KERNEL
  38. #include "ixemul.h"
  39. #include <sys/wait.h>
  40. #include "kprintf.h"
  41.  
  42. extern struct ExecBase *SysBase;
  43. /* this just jumps right back into ix_startup() */
  44.  
  45.  
  46. /*
  47.  * Note: I kept the partition into startup and _main(), although in this
  48.  *       case, both functions could be done in one function, since this is
  49.  *       a library, and the user can't override _main anyway but globally...
  50.  */
  51.  
  52. int
  53. ix_startup (char *aline, int alen,
  54.         int expand, char *wb_default_window, u_int main, int *real_errno)
  55. {
  56.   struct Process *me = (struct Process *) SysBase->ThisTask;
  57.   int exit_val;
  58.   struct WBStartup *wb_msg = NULL;
  59.   int fd;
  60.  
  61.   /*
  62.    * The following code to reset the fpu might not be necessary, BUT since
  63.    * a CLI shell doesn't spawn a new process when executing a command - it 
  64.    * insteads calls the command like a subroutine - it depends on the Shell
  65.    * whether the fpu is setup correctly. And I don't like to depend on any
  66.    * thing ;-)
  67.    */
  68.  
  69.   if (gotanfpu())
  70.     resetfpu();
  71.   /* first deal with WB messages, since those HAVE to be answered properly,
  72.    * even if we should fail later (memory, whatever..) */
  73.  
  74.   if (! me->pr_CLI)
  75.     {
  76.       /* we have been started by Workbench. Get the StartupMsg */
  77.       WaitPort (& me->pr_MsgPort);
  78.       wb_msg = (struct WBStartup *) GetMsg (& me->pr_MsgPort);
  79.       /* further processing in _main () */
  80.     }
  81.   else
  82.     {
  83.       /* for usage by sys_exit() for example */
  84.       KPRINTF (("CLI command line '%s'\n", aline));
  85.       u.u_argline = aline;
  86.       u.u_arglinelen = alen;
  87.     }
  88.  
  89.  
  90.   u.u_expand_cmd_line = expand;
  91.   if (real_errno) u.u_errno = real_errno;
  92.   KPRINTF (("&errno = %lx\n", real_errno));
  93.  
  94.   exit_val = _setjmp (u.u_jmp_buf);
  95.   if (! exit_val)
  96.     {
  97.       /* from now on it's safe to allow signals */
  98.       syscall (SYS_sigsetmask, 0);
  99.       /* the first time thru call the program */
  100.       KPRINTF (("calling __main()\n"));
  101.       exit_val = me->pr_CLI ? syscall (SYS__main, aline, alen, main)
  102.                 : syscall (SYS__main, wb_msg, wb_default_window, main);
  103.       KPRINTF (("exit_val = %ld\n", exit_val));
  104.     }
  105.   else
  106.     /* in this case we came from a longjmp-call */
  107.     {
  108.       exit_val = u.p_xstat;
  109.     }
  110.   ix_remove_sigwinch ();
  111.  
  112.   /* had to move the closing of files out of ix_close(), as close() may 
  113.      actually wait for the last packet to arrive, and inside ix_close() we're
  114.      inside Forbid, and may thus never wait! */
  115.  
  116.   /* close all files */
  117.   for (fd = 0; fd < NOFILE; fd++) 
  118.     if (u.u_ofile[fd]) syscall (SYS_close, fd);
  119.  
  120.   /* if at all possible, free memory before entering Forbid ! (Semaphore
  121.      problems..) */
  122.   all_free ();
  123.   /* if started from workbench, Forbid(), since on reply WB will deallocate
  124.    * our task... */
  125.   if (! me->pr_CLI)
  126.     {
  127.       Forbid ();
  128.       ReplyMsg ((struct Message *) wb_msg);
  129.     }
  130.  
  131.   return WEXITSTATUS(exit_val);
  132. }
  133.  
  134. void
  135. _exit (int retcode)
  136. {
  137.   /* ignore all signals from now on */
  138.   syscall (SYS_sigsetmask, ~0);
  139.   u.p_xstat = W_EXITCODE(retcode, 0);
  140.   _longjmp (u.u_jmp_buf, 1);
  141. }
  142.